/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.base.service.impl;

import java.util.Collection;
import java.util.List;

import com.xy.tool.minio.domain.constant.BucketConstant;
import com.xy.tool.minio.util.MinioUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xy.lang.domain.Result;
import com.xy.biz.base.domain.bo.AttachmentBO;
import com.xy.biz.base.domain.query.AttachmentQuery;
import com.xy.biz.base.service.AttachmentService;
import com.xy.biz.base.manager.AttachmentManager;

/**
 *
 *
 * @author wwj
 * @date 2021-4-6
 */
@Service
public class AttachmentServiceImpl implements AttachmentService {

	@Autowired
	private AttachmentManager attachmentManager;
	@Autowired
	private MinioUtil minioUtil;

	@Override
	public AttachmentBO saveAttachment(AttachmentBO attachmentBO) {
		return attachmentManager.saveAttachment(attachmentBO);
	}

	@Override
	public Result<?> saveAllAttachment(Collection<AttachmentBO> collection) {
		int affectedRows = attachmentManager.saveAllAttachment(collection);
		if (affectedRows > 0) {
			return Result.success();
		}
		return Result.ofErrorRowsAffected();
	}

	@Override
	public Result<?> updateAttachment(AttachmentBO attachmentBO) {
		int affectedRows = attachmentManager.updateAttachment(attachmentBO);
		if (affectedRows > 0) {
			return Result.success();
		}
		return Result.ofErrorRowsAffected();
	}

	@Override
	public Result<?> deleteAttachment(Long attachmentId) {
		int affectedRows = attachmentManager.deleteAttachment(attachmentId);
		if (affectedRows > 0) {
			return Result.success();
		}
		return Result.ofErrorRowsAffected();
	}

	@Override
	public AttachmentBO getAttachment(Long attachmentId) {
		return attachmentManager.getAttachment(attachmentId);
	}

	@Override
	public List<AttachmentBO> listAttachmentById(Collection<Long> collection) {
		return attachmentManager.listAttachmentById(collection);
	}

	@Override
	public List<AttachmentBO> listAttachment(AttachmentQuery query) {
		List<AttachmentBO> attachmentBOS = attachmentManager.listAttachment(query);
		attachmentBOS.parallelStream().forEach(ab -> {
			try {
				ab.setObjectUrl(minioUtil.getObjectURL(BucketConstant.ATTACHMENT_BUCKET,ab.getFileName(),null));
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		return attachmentBOS;
	}

	@Override
	public int countAttachment(AttachmentQuery query) {
		return attachmentManager.countAttachment(query);
	}

}
